//
//  YZMCheckService.m
//  YZMicroStock
//
//  Created by apple on 16/12/14.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMCheckService.h"
#import "YZMAlertView.h"
#import "YZClient.h"
#import "YZMWebViewController.h"

@implementation YZMCheckService

+ (instancetype)sharedService {
    
    static YZMCheckService *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[self alloc] init];
    });
    
    return instance;
}

- (void)checkForOneYuan{
    //是否登录
    if (!YZMUserServiceInstance.currentUser) {

        [YZMUserService showLoginCtrlWithSourcePageName:@"webView"];
        return;
    }
    [YZMHCMInstance pushViewControllerWithName:@"oneYuan" andParams:nil];
}

- (void)checkForTD{
    if (!YZMUserServiceInstance.currentUser) {
        
        [YZMUserService showLoginCtrlWithSourcePageName:@"webView"];
        return;
    }
    [YZMHCMInstance pushViewControllerWithName:@"stockTD" andParams:nil];
}

- (void)checkForFreeExpericence{
    //是否登录
    if (!YZMUserServiceInstance.currentUser) {
        
        [YZMUserService showLoginCtrlWithSourcePageName:@"webView"];
        return;
    }
    [YZMHCMInstance pushViewControllerWithName:@"freeExperience" andParams:nil];
}

-(void)checkForSimulate{
    
    //判断是否登录
    if (!YZMUserServiceInstance.currentUser) {
        [YZMUserService showLoginCtrlWithSourcePageName:@"webView"];
        return;
    }
    //实名认证判断
    if (!YZMUserServiceInstance.user.detail.isNamed) {
        YZMAlertView *alertView = [YZMAlertView alertViewForRecommandWithContent:@"您还未实名认证，请先认证"];
        alertView.cancelButton.hidden = NO;
        [alertView.sureButton setTitle:@"立即验证" forState:UIControlStateNormal];
        alertView.confirmHandle = ^{
            [YZMHCMInstance pushViewControllerWithName:@"identifyVerify" andParams:nil];
            return YES;
        };
        [alertView showInView:YZMHCMInstance.currentNavationCtroller.view];
        return;
    }
    [YZClient simU_getBasicDataSuccess:^(NSURLResponse *response, id responseObject) {
        YZGeneralResultModel *topmodel=responseObject;
        if (topmodel.success) {
            NSDictionary *dic=topmodel.data;
            BOOL strategyUserFlag=[[dic objectForKey:@"strategyUserFlag"]boolValue];
            if (strategyUserFlag) {
                [YZMHCMInstance pushViewControllerWithName:@"Simulate" andParams:nil];
            }else{
                [self enterSimulateRequest];
            }
        }else{
            [topmodel showMessage];
        }
    } failure:^(NSError *error) {
        [error yz_showMessage];
    }];

}

-(void)enterSimulateRequest{

    [YZClient simU_EnterUsersuccess:^(NSURLResponse *response, id responseObject) {
        YZGeneralResultModel *topmodel=responseObject;
        if (topmodel.success) {
            
        [[YZMAlertView alertViewForRecommandWithContent:@"报名成功"] showInView:YZMHCMInstance.currentNavationCtroller.view];

            
            YZMWebViewController *vc = [YZMHCMInstance instanceWithControllerName:@"webView"];
            if (vc) {
                [[vc webView] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[vc webView].request.URL.absoluteString stringByAppendingFormat:@"&v=%d",rand()]]]];
            }
        }else{
            [topmodel showMessage];
        }
    } failure:^(NSError *error) {
        [error yz_showMessage];
    }];
}


@end
